package com.guo.hui.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guo.hui.dao.shiro.TpPermissionMapper;
import com.guo.hui.dao.shiro.TpRolePermissionMapper;
import com.guo.hui.model.shiro.TpPermission;
import com.guo.hui.model.shiro.TpRole;
import com.guo.hui.model.shiro.TpRolePermission;
import com.guo.hui.model.shiro.TpUserRole;
import com.guo.hui.page.PageParam;
import com.guo.hui.service.PermissionService;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private TpPermissionMapper permisDao;
	@Autowired
	private TpRolePermissionMapper rolePermisDao;

	@Override
	public List<TpPermission> selectByMap(Map<String, Object> map) {

		return permisDao.selectByMap(map);
	}

	@Override
	public void savePermission(TpPermission tp) {
		tp.setCreateDate(new Date());
		tp.setModifyDate(new Date());
		permisDao.insert(tp);
	}

	@Override
	public void updatePermission(TpPermission tp) {
		tp.setModifyDate(new Date());
		permisDao.update(tp);
	}

	@Override
	public PageInfo<TpPermission> selectByMapPage(PageParam pageParm) {
		PageHelper.startPage(pageParm.getPage(), pageParm.getPageSize());
		List<TpPermission> list = permisDao.selectByMap(pageParm.getSearchContent());
		return new PageInfo<TpPermission>(list);
	}

	@Override
	public void saveRolePermis(Integer roleid, Integer[] ids) {
		Map<String, Object> map = new HashMap<>();
		map.put("roleId", roleid);
		List<TpRolePermission> listRolePermis = rolePermisDao.selectByMap(map);
		if (listRolePermis != null && listRolePermis.size() > 0) {
			// 原本的角色ID集合
			List<Integer> oldPermids = new ArrayList<>();
			for (TpRolePermission tp : listRolePermis) {
				oldPermids.add(tp.getPermissionId());
			}
			// 新选择的角色ID集合
			List<Integer> newPermids = Arrays.asList(ids);
			// 是否删除
			for (int i = 0, j = listRolePermis.size(); i < j; i++) {
				if (!newPermids.contains(listRolePermis.get(i).getRoleId())) {
					// 删除旧的数据 可使用批量操作
					rolePermisDao.deleteById(listRolePermis.get(i).getId());
				}
			}
			// 是否添加
			for (int m = 0, n = newPermids.size(); m < n; m++) {
				if (!oldPermids.contains(newPermids.get(m))) {
					rolePermisDao.insert(getRolePermis(roleid, newPermids.get(m)));
				}
			}
		} else {
			for (Integer permid : ids) {
				rolePermisDao.insert(getRolePermis(roleid, permid));
			}
		}
	}

	private TpRolePermission getRolePermis(Integer roleId, Integer permid) {
		TpRolePermission tpRolePermission = new TpRolePermission();
		tpRolePermission.setRoleId(roleId);
		tpRolePermission.setPermissionId(permid);
		return tpRolePermission;
	}

}
